package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysChannel;
import com.ruoyi.system.service.ISysChannelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 渠道Controller
 * 
 * @author ruoyi
 * @date 2022-01-17
 */
@Controller
@RequestMapping("/system/channel")
public class SysChannelController extends BaseController
{
    private String prefix = "system/channel";

    @Autowired
    private ISysChannelService sysChannelService;

    @RequiresPermissions("system:channel:view")
    @GetMapping()
    public String channel()
    {
        return prefix + "/channel";
    }

    /**
     * 查询渠道列表
     */
    @RequiresPermissions("system:channel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysChannel sysChannel)
    {
        startPage();
        List<SysChannel> list = sysChannelService.selectSysChannelList(sysChannel);
        return getDataTable(list);
    }

    /**
     * 导出渠道列表
     */
    @RequiresPermissions("system:channel:export")
    @Log(title = "渠道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysChannel sysChannel)
    {
        List<SysChannel> list = sysChannelService.selectSysChannelList(sysChannel);
        ExcelUtil<SysChannel> util = new ExcelUtil<SysChannel>(SysChannel.class);
        return util.exportExcel(list, "渠道数据");
    }

    /**
     * 新增渠道
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存渠道
     */
    @RequiresPermissions("system:channel:add")
    @Log(title = "渠道", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysChannel sysChannel)
    {
        if(sysChannelService.checkChannelByCode(sysChannel.getCode()) != null){
            return  error("渠道编号已经存在："+sysChannel.getCode());
        }
        sysChannel.setCreateUser(getLoginName());
        return toAjax(sysChannelService.insertSysChannel(sysChannel));
    }

    /**
     * 修改渠道
     */
    @RequiresPermissions("system:channel:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysChannel sysChannel = sysChannelService.selectSysChannelById(id);
        mmap.put("sysChannel", sysChannel);
        return prefix + "/edit";
    }

    /**
     * 修改保存渠道
     */
    @RequiresPermissions("system:channel:edit")
    @Log(title = "渠道", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysChannel sysChannel)
    {
        SysChannel temp = sysChannelService.checkChannelByCode(sysChannel.getCode());
        if(temp!= null && temp.getId().longValue() != sysChannel.getId().longValue()){
            return  error("渠道编号已经存在："+sysChannel.getCode());
        }
        sysChannel.setUpdateUser(getLoginName());
        return toAjax(sysChannelService.updateSysChannel(sysChannel));
    }

    /**
     * 删除渠道
     */
    @RequiresPermissions("system:channel:remove")
    @Log(title = "渠道", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysChannelService.deleteSysChannelByIds(ids));
    }
}
